package chat.user;

import java.net.Socket;
import java.util.Vector;
import chat.client.ClientMain;
import chat.server.GroupProcessor;
import chat.server.SocketProcessor;


public class MessageCreator {
	
	private User user;
	private String username;
	
	public MessageCreator(User u){
		
		this.user =u;
	}
	
	public static RequestGroupUpdate createUpdateGroupMessage(User u){
		
		RequestGroupUpdate m = new RequestGroupUpdate();
		m.setUser(u);
		
		return m;
	}
	
	public static Request createTextMessage(String text, User u, String destination_user){
			
		if (destination_user.equalsIgnoreCase(u.getRelatedGroup())){
		
		RequestMessage r = new RequestMessage(u);
		r.setMessage(text);
		return r;
		}
		else {
			RequestUserMessage r = new RequestUserMessage(destination_user, u);
			r.setMessage(text);
			return r;
		}
		
		
	}
	
	public static RequestMessage sendTextMessage(String text, User user, String from_user) {

		RequestMessage r = new RequestMessage(user);
		r.setMessage(text);
		r.setFromUser(from_user);
		return r;
	}
	
	public static boolean RequestNewGroup (String group_name, User u){
		
		Request r = new RequestNewGroup();
		r.setUser(u);
		r.setGroup(group_name);
		
		return ClientMain.addOutMessage(r);
	}
	
	public static boolean AuthRequest(User u){
		
		Request r = new RequestAuth ();
		
		r.setUser(u);
		
		return ClientMain.addOutMessage(r);
	}
	
	public static boolean ConnectGroupRequest(String group_name, User user){
		
		RequestConnectGroup r = new RequestConnectGroup(user);
		
		r.setGroup(group_name);
		
		return ClientMain.addOutMessage(r);
	}
	
	public static RequestConnectGroup ConnectGroupRequestResponse(User user, String group){
		
		RequestConnectGroup r = new RequestConnectGroup (user);
		r.setGroup(group);
		
		return r;
	}
	
	public static Request AuthRequestResponse (User user){
		
		Request r = new RequestAuth(user);
		
		return r;
	}
	
	public static RequestGroupUpdate GroupUpdateRequestResponse (User user, Vector<Group> group_names){
		
		RequestGroupUpdate r = new RequestGroupUpdate();
		r.setUser(user);
		r.setGroupNames (group_names);
		
		return r;
	}
	
	public static boolean RemoveGroupRequest (User u, String group){
		
		RequestRemoveGroup r = new RequestRemoveGroup(group);
		r.setUser(u);
		
		return ClientMain.addOutMessage(r);
	}
	
	public static RequestRemoveGroup RemoveGroupRequestResponse (User user, boolean allow, String answer){
		
		RequestRemoveGroup r = new RequestRemoveGroup (allow, answer);
		
		r.setUser(user);
		
		return r;
	}
	
	public static void requestUserGroupUpdate (User u){
		
		RequestUserGroupUpdate r = new RequestUserGroupUpdate(u);
		
		ClientMain.addOutMessage(r);
	}
	
	public static void requestChangedGroupUser (User u, String new_group, String old_group){
		
		RequestChangeUserGroup r = new RequestChangeUserGroup(u, new_group, old_group);
		
		ClientMain.addOutMessage(r);
	}
	
	public static void requestChangedGroupUserResponse (User destination_user, String new_group, String old_group, User from_user){
		
		RequestChangeUserGroup r = new RequestChangeUserGroup(destination_user, new_group, old_group, from_user);
		
		SocketProcessor.addMessageOutQueue(r);
	}
	
	public static void requestUserStatusUpdate (User user){
		RequestStatusUpdate r = new RequestStatusUpdate(user);
		ClientMain.addOutMessage(r);
	}
	
	public static Request requestUserStatusUpdateResponse (String new_status, String from_user, User user){
		RequestStatusUpdate r = new RequestStatusUpdate(new_status);
		r.setFromUser(from_user);
		r.setUser(user);
		return r;
	}
	public static void requestDisconnect (User user){
		RequestDisconnect r = new RequestDisconnect(user);
		ClientMain.addOutMessage(r);	}
	
	public static Request requestDisconnectResponse (User user){
		RequestDisconnect r = new RequestDisconnect(user);
		return r;
	}
	
	
}
